
import java.io.*;

/**
 *Nice code to execute the shell scripts via java
 * @author Pankaj Soni
 */
public class RunShellCommand {

    /**
     * @param args the command line arguments
     * Required: args[0] must be the script to be executed
     * Optional: args[1] to ... will be the parameters to be given to the script
     * name present in the args[0]
     * Example:
     * args[0]: /home/oracle/sample.ksh
     * args[1]: Pankaj
     *
     * java RunShellCommand /home/oracle/sample.ksh Pankaj
     */
    public static void main(String[] args) throws Exception {
        /* java.util.Map<String, String> env = pb.environment();
        env.put("VAR1", "myValue");
        env.remove("OTHERVAR");
        env.put("VAR2", env.get("VAR1") + "suffix");
        pb.directory(new File("myDir"));
         */
        ProcessBuilder pb = new ProcessBuilder(args);
        Process p = pb.start();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String temp = "";
        while ((temp = br.readLine()) != null) {
            System.out.println(temp);
        }
    }
}
